package com.i2works.smartluolong.manage.controller.common;

import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.common.BaseUtils;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.common.HoliDayQuery;
import com.i2works.smartluolong.web.service.common.IHolidayService;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 节假日控制层
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/6/19 14:46
 */

@Controller
@RequestMapping("/itemHoliday")
public class HolidayController extends BaseModule {

    @Autowired
    private IHolidayService holidayService;

    @GetMapping("/view/index")
    public String index(Model model) {
        try {
            List<String> holidayList = holidayService.queryAll(new HoliDayQuery());
            if (CollectionUtil.isNotEmpty(holidayList)) {
                model.addAttribute("holidayList", BaseUtils.listToStr(holidayList));
            }
        } catch (ServiceException e) {
            logger.error("节假日页面异常：" + e.getMessage());
        }
        return "/holiday/index";
    }

    /**
     * 接口
     */
    @GetMapping("/view/api")
    public Result api(HoliDayQuery holiDayQuery) {
        return R.success();
    }


    /**
     * 添加
     */
    @PostMapping("/save/action")
    @ResponseBody
    public Result save(@RequestParam("dateStr") String dateStr) {
        String[] dateList = dateStr.split(",");
        try {
            holidayService.deleteAll();
            holidayService.saveAll(dateList);
        } catch (ServiceException e) {
            logger.error("添加节假日异常：" + e.getMessage());
            return R.error(MsgEnum.SAVE_ERROR.getMsg());
        }
        return R.success();
    }
}
